/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.ims.modules.wechat.entity.Questionnaire;
import com.boxin.ims.modules.wechat.entity.QuestionnaireReplyHuman;
import com.boxin.ims.modules.wechat.service.QuestionnaireReplyHumanService;
import com.boxin.ims.modules.wechat.service.QuestionnaireService;

/**
 * 调查问卷回答的用户Controller
 * @author XiaoQian
 * @version 2014-05-10
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/wechat/questionnaireReplyHuman")
public class QuestionnaireReplyHumanController extends BaseController {

	@Autowired
	private QuestionnaireReplyHumanService questionnaireReplyHumanService;
	
	@Autowired
	private QuestionnaireService questionnaireService;
	
	@ModelAttribute
	public QuestionnaireReplyHuman get(@RequestParam(required=false) Long id) {
		if (id != null){
			return questionnaireReplyHumanService.get(id);
		}else{
			return new QuestionnaireReplyHuman();
		}
	}
	
	@RequestMapping(value = {"list", ""})
	public String list(QuestionnaireReplyHuman questionnaireReplyHuman, HttpServletRequest request, HttpServletResponse response, Model model) {
		String questionnaireId=request.getParameter("questionnaireId");
		Questionnaire questionnaire = null;
		if(questionnaireId!=null){
			questionnaire=questionnaireService.get(Long.parseLong(questionnaireId));
			questionnaireReplyHuman.setQuestionnaire(questionnaire);
		}
		User user = UserUtils.getUser();
        Page<QuestionnaireReplyHuman> page = questionnaireReplyHumanService.find(new Page<QuestionnaireReplyHuman>(request, response), questionnaireReplyHuman); 
        model.addAttribute("page", page);
		return "modules/wechat/questionnaireReplyHumanList";
	}

	@RequiresPermissions("wechat:questionnaireReplyHuman:view")
	@RequestMapping(value = "form")
	public String form(QuestionnaireReplyHuman questionnaireReplyHuman, Model model) {
	
		User user = UserUtils.getUser();
		
		model.addAttribute("questionnaireReplyHuman", questionnaireReplyHuman);
		return "modules/wechat/questionnaireReplyHumanForm";
	}

	@RequiresPermissions("wechat:questionnaireReplyHuman:edit")
	@RequestMapping(value = "save")
	public String save(QuestionnaireReplyHuman questionnaireReplyHuman, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, questionnaireReplyHuman)){
			return form(questionnaireReplyHuman, model);
		}
		questionnaireReplyHumanService.save(questionnaireReplyHuman);
		addMessage(redirectAttributes, "保存调查问卷回答的用户'" + questionnaireReplyHuman.getName() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/modules/wechat/questionnaireReplyHuman/?repage";
	}
	
	@RequiresPermissions("wechat:questionnaireReplyHuman:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		questionnaireReplyHumanService.delete(id);
		addMessage(redirectAttributes, "删除调查问卷回答的用户成功");
		return "redirect:"+Global.ADMIN_PATH+"/modules/wechat/questionnaireReplyHuman/?repage";
	}

}
